package com.hcbd.tdwt.ui.htct.fragment;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.ui.haige.common.fragment.BdStatusFragment_;
import com.hcbd.tdwt.ui.haige.common.fragment.PositionInfoFragment_;

import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.Listener.InfoInterface;
import com.hcbd.tdwt.util.StringUtil;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGGAInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGSVInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDRMCInfo;
import com.hcbd.tdwt.ui.xingyu.fragment.SatelliteListFragment_;
import com.hcbd.tdwt.ui.xingyu.fragment.SkyMapFragment_;
import com.hcbd.tdwt.ui.xingyu.vo.SatelliteVO;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.activity_position_info_fuda)
public class PositionFragment extends BaseFragment {
    @ViewById(R.id.position_info)
    RadioButton positionInfoRB;
    @ViewById(R.id.sky_map)
    RadioButton skyMapRB;
    @ViewById(R.id.satellite_list)
    RadioButton satelliteRB;
    @ViewById(R.id.bd_status)
    RadioButton bdStatusRB;
    @ViewById
    TextView title;
    @ViewById
    RelativeLayout topRL;
    @ViewById
    RadioGroup positionRG;
    @ViewById
    ImageButton headLeft;
    PositionInfoFragment_ positionInfoFragment;
    SatelliteListFragment_ satelliteListFragment;
    SkyMapFragment_ skyMapFragment;
    private List<SatelliteVO> satelliteVOList;
    private TreeMap<String, SatelliteVO> hashMap = new TreeMap<>();


    @AfterViews
    protected void init() {
        title.setText("卫星");
        headLeft.setVisibility(View.GONE);
        positionInfoFragment = new PositionInfoFragment_();
        satelliteListFragment = new SatelliteListFragment_();
        skyMapFragment = new SkyMapFragment_();
        satelliteVOList = new ArrayList<>();
        setSkyInfoInterface(skyMapFragment);
        setSatelliteInfoInterface(satelliteListFragment);
        setPositionInterface(positionInfoFragment);
        initMsgRegister();
        positionInfoRB.setChecked(true);
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDGSVInfo bdgsvInfo){
        initData(bdgsvInfo);
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDGGAInfo bdggaInfo){
        positionInterface.onGGAInfo(bdggaInfo);
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDRMCInfo bdrmcInfo){
        positionInterface.onRMCInfo(bdrmcInfo);
    }

    @CheckedChange(R.id.position_info)
    void positionInfoRB(boolean isChecked){
        if(isChecked) {
            switchFragment(positionInfoFragment, R.id.resultLL);
        }
    }
    @CheckedChange(R.id.sky_map)
    void skyMapRB(boolean isChecked){
        if(isChecked) {
            switchFragment(skyMapFragment, R.id.resultLL);
        }
    }
    @CheckedChange(R.id.satellite_list)
    void satelliteRB(boolean isChecked){
        if(isChecked) {
            switchFragment(satelliteListFragment, R.id.resultLL);
        }
    }

    private InfoInterface skyInfoInterface;
    private InfoInterface satelliteInfoInterface;
    private InfoInterface positionInterface;

    public void setSkyInfoInterface(InfoInterface infoInterface){
        this.skyInfoInterface=infoInterface;
    }
    public void setSatelliteInfoInterface(InfoInterface infoInterface){
        this.satelliteInfoInterface=infoInterface;
    }
    public void setPositionInterface(InfoInterface infoInterface){
        this.positionInterface=infoInterface;
    }
    public void initData(BDGSVInfo info){
        SatelliteVO satelliteVO=new SatelliteVO();
        if(!"".equals(info.sv1)&& StringUtil.isNumeric(info.sv1)&&!"".equals(info.azl1)&&!"".equals(info.elv1)){
            satelliteVO.setSv(info.sv1);
            satelliteVO.setCno(info.cno1);
            satelliteVO.setAz(info.azl1);
            satelliteVO.setElv(info.elv1);
            satelliteVO.setSatelliteType(info.satellitesType);
            hashMap.put(info.satellitesType+info.sv1,satelliteVO);
        }
        if(!"".equals(info.sv2)&&StringUtil.isNumeric(info.sv1)&&!"".equals(info.azl2)&&!"".equals(info.elv2)) {
            SatelliteVO satelliteVO2 = new SatelliteVO();
            satelliteVO2.setSv(info.sv2);
            satelliteVO2.setCno(info.cno2);
            satelliteVO2.setAz(info.azl2);
            satelliteVO2.setElv(info.elv2);
            satelliteVO2.setSatelliteType(info.satellitesType);
            hashMap.put(info.satellitesType+info.sv2, satelliteVO2);
        }
        if(!"".equals(info.sv3)&&StringUtil.isNumeric(info.sv1)&&!"".equals(info.azl3)&&!"".equals(info.elv3)) {
            SatelliteVO satelliteVO3 = new SatelliteVO();
            satelliteVO3.setSv(info.sv3);
            satelliteVO3.setCno(info.cno3);
            satelliteVO3.setAz(info.azl3);
            satelliteVO3.setElv(info.elv3);
            satelliteVO3.setSatelliteType(info.satellitesType);
            hashMap.put(info.satellitesType+info.sv3, satelliteVO3);
        }
        if(!"".equals(info.sv4)&&StringUtil.isNumeric(info.sv1)&&!"".equals(info.azl4)&&!"".equals(info.elv4)) {
            SatelliteVO satelliteVO4 = new SatelliteVO();
            satelliteVO4.setSv(info.sv4);
            satelliteVO4.setCno(info.cno4);
            satelliteVO4.setAz(info.azl4);
            satelliteVO4.setElv(info.elv4);
            satelliteVO4.setSatelliteType(info.satellitesType);
            hashMap.put(info.satellitesType+info.sv4, satelliteVO4);
        }
        satelliteVOList.clear();
        for (String key : hashMap.keySet()) {
            satelliteVOList.add(hashMap.get(key));
        }
        skyInfoInterface.onGSVInfo(satelliteVOList);
        satelliteInfoInterface.onGSVInfo(satelliteVOList);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Override
    public void  onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(currentFragment  instanceof BdStatusFragment_){
            currentFragment.onHiddenChanged(hidden);
        }
        if(hidden){
            //不可见
            hideInputKeyBord();
            EventBus.getDefault().unregister(this);
        }else{
            initMsgRegister();

        }
    }

}
